<template>
  <div class="indexPage">
    <div class="header flexBetween marginB20">
      <img class="logo" :src="data.logoUrl" alt="">
      <div class="flex">
        <img :src="selectToLabel(GameLang, data.gameLang, 'icon', 'value')" alt="" style="width: 30px;height: 20px;">
        <p class="gameTitle marginL5">{{ selectToLabel(GameLang, data.gameLang, 'label', 'value') }}</p>
        <div class="icon marginL5" @click="data.isShowLang = !data.isShowLang"><img :src="Lang" alt=""></div>
      </div>
      <div class="box_lang" v-show="data.isShowLang">
        <div class="flex lang_item" v-for="(item, index) in GameLang" :key="item.value" @click="chooseLang(item)">
          <img :src="item.icon" alt="">
          <p class="width80">{{ item.label }}</p>
        </div>
      </div>
    </div>
    <div class="box_round">
      <swiper class="mySwiper" :slidesPerView="1" :spaceBetween="50" :loop="true" :modules="modules"
        :pagination="{ clickable: true }" :autoplay="{ delay: 2000, disableOnInteraction: false }">
        <swiper-slide v-for="(item, index) in RoundList" :key="index">
          <img class="round_img" :src="item" alt="">
        </swiper-slide>
      </swiper>
    </div>
    <!-- 游戏类型 -->
    <div class="flex marginB10 box_type scrollDivStyle">
      <div class="textCenter gameItem" :class="data.gameType == item.value ? 'selectColorMobileType' : ''"
        v-for="(item, index) in GameType" @click="chooseType(item)">
        <div class="textCenter"><img :src="item.icon" alt=""></div>
        <p>{{ item.label }}</p>
      </div>
    </div>
    <!-- 搜索 -->
    <div class="box_brand marginB10">
      <!-- <div class="flex scrollDivStyle">
        <div class="textCenter game_brand marginR10" :class="data.indexBrand == index ? 'selectColorMobileGame animate__bounceIn' : ''"
          v-for="(item, index) in GameBrand" @click="chooseBrand(item, index)" v-show="item.show">
          <img :src="item.icon" alt="" :class="{ 'marginT2': index != 5 }">
        </div>
      </div> -->
    </div>
    <!-- 热门游戏展示 -->
    <div class="flexBetween">
      <p class="box-title">JACKPOT <span class="box-text">{{ HotGame.length }} Games</span></p>
      <!-- <p class="box-title" @click="chooseGameShow('JACKPOT Games', HotGame)">
        <span class="box-text colorBurl">View all</span>
      </p> -->
    </div>
    <div class="flex marginB20 hotGame scrollDivStyle" ref="scrollContainer" @wheel="handleWheel">
      <div class="choose_game" v-for="(item, index) in HotGame" :key="index" @click="chooseGame(item)">
        <div class="marginR10 gameItem text-cut">
          <img class="gameImg" :src="item.label" alt="">
          <p class="gameTitle text-cut">{{ item.remark }}</p>
          <p class="remark">{{ item.brand }} Game</p>
        </div>
      </div>
    </div>
    <!-- 品牌游戏 -->
    <div class="width100 marginB20" v-for="(item, index) in data.list" :key="index" :id="selectToLabel(GameBrand, item.brand)" style="display: block !important;">
      <div class="flexBetween" v-show="data.gameType == 1 || item.value == data.gameType ? true : false">
        <p class="box-title marginB5" >{{ item.remark }} <span class="box-text">{{ item.label.length }} Games</span></p>
        <!-- <p class="box-title game_all" @click="chooseGameShow(item.remark, item.label)">
          <span class="box-text colorBurl">{{ data.isShowGame }} View all</span></p> -->
      </div>
      <div class="flex width100 hotGame scrollDivStyle" v-show="data.gameType == 1 || item.value == data.gameType ? true : false">
        <div class="choose_game" v-for="(second, two) in item.label" :key="two" @click="chooseGame(second)">
          <div class="width100 marginR10 gameItem text-cut" >
            <img class="gameImg" :src="second.label" alt="">
            <p class="width100 gameTitle text-cut">{{ second.remark }}</p>
            <p class="remark">{{ item.remark }}</p>
          </div>
        </div>
      </div>
    </div>
    <!-- 加载中 -->
    <div id="layer-loading" class="layui-m-layer layui-m-layer2" index="0" v-if="data.loading">
      <div class="layui-m-layershade"></div>
      <div class="layui-m-layermain">
        <div class="layui-m-layersection">
          <div class="layui-m-layerchild layui-m-anim-scale">
            <div class="layui-m-layercont">
              <i></i>
              <i class="layui-m-layerload"></i>
              <i></i>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- 展示全部游戏 -->
    <el-dialog v-model="data.isShowGame" title="游戏" :close-on-click-modal="false" :close-on-press-escape="false"
      width="95%" top="12vh" @close="data.isShowGame = false">
      <div class="dialogMaxHeight">
        <p class="box-title">{{ data.gameTypeName }}</p>
        <div class="choose_game" v-for="(item, two) in data.gameTypeList" :key="two" @click="chooseGame(item)">
          <div class="width100 marginR10 gameItem text-cut">
            <img class="gameImg" :src="item.label" alt="">
            <p class="width100 gameTitle text-cut">{{ item.remark }}</p>
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import {
  GameType, GameBrand, GamePP, GamePG, GameJILI, LogoUrl,GameMine,
  GameOMG, GameSPRIBE, HotGame, GameLang, RoundList
} from '@/config/eunm'
import { throttle, selectToLabel, getOperatorInfo } from '@/utils/index'
import { getGame, playGame } from '@/api/game'
import { userLogin } from '@/api/user'

import { Swiper, SwiperSlide } from 'swiper/vue';
import { Autoplay, Pagination, A11y } from 'swiper/modules';
import 'swiper/css';
import 'swiper/css/pagination';
const modules = [Autoplay, Pagination, A11y]

// Icon
import Me from "@/assets/icon/yanshi_com_icon11.png" //我的
import News from "@/assets/icon/yanshi_com_icon13.png" //消息
import Lang from "@/assets/icon/yanshi_com_icon12.png" //语言
import Searchs from "@/assets/icon/yanshi_com_icon15.png" //搜索
import Back from "@/assets/icon/yanshi_zhiding.png" //回到顶部
import Magnifying from "@/assets/icon/yanshi_com_icon16.png" //放大镜

const data = reactive({
  // iconList: [Me, News, Lang],
  iconList: [Lang],
  gameType: 1,
  indexBrand: -1,
  gameName: '',
  searchGame: -1,
  gameLang: 'en',
  isShowLang: false,
  list: [
    // { label: GameOMG, value: 3, remark: 'Slots Games', brand: 'OMG', show: true }, 
    // { label: GameSPRIBE, value: 3, remark: 'Mini Games', brand: 'SPRIBE', show: true },
    // { label: GamePP, value: 2, remark: 'PP Games', brand: 'PRAGMATIC', show: true },
    // { label: GameHacksaw, value: 2, remark: 'Hacksaw Games', brand: 'HACKSAW', show: true },
    // { label: GameOMG, value: 3, remark: 'OMG Games', brand: 'OMG', show: true },
    // { label: GameSPRIBE, value: 3, remark: 'SPRIBE Games', brand: 'SPRIBE', show: true },
    // { label: GameJILI, value: 2, remark: 'JILI Games', brand: 'JILI', show: true },
    // { label: GamePG, value: 2, remark: 'PG Games', brand: 'PG', show: true },
    // { label: GamePP, value: 2, remark: 'PP Games', brand: 'PRAGMATIC', show: true },
    { label: GameOMG, value: 1, remark: selectToLabel(LogoUrl,getOperatorInfo(location.hostname),'remark','value')+' Games', brand: selectToLabel(LogoUrl,getOperatorInfo(location.hostname),'remark','value') },
    { label: GamePG, value: 2, remark: 'PG Games', brand: 'PG' },
    { label: GameJILI, value: 2, remark: 'JILI Games', brand: 'JILI' },
    { label: GameMine, value: 3, remark: 'Mine Games', brand: 'MineGame' },
    { label: GamePP, value: 2, remark: 'PP Games', brand: 'PRAGMATIC' },
    { label: GameSPRIBE, value: 3, remark: 'SPRIBE Games', brand: 'SPRIBE' },
  ],
  addId: 0,
  liveGameData: [] as any,
  androidShow: false,
  windowShow: false,
  iphoneShow: false,
  loading: false,
  scrollTopValue: 0,
  logoUrl: selectToLabel(LogoUrl, getOperatorInfo(location.hostname)),
  isShowGame: false,
  gameTypeName: '',
  gameTypeList: [] as any[],
})

const FirstMeth = async () => {
  // 动态监听滚动条高度
  const scrollContainer = document.querySelector('#app') as any;
  if (scrollContainer) {
    scrollContainer.addEventListener('scroll', getScroll)
  }
  getGameList()
  getPhoneInfo()
}

// 获取可玩游戏
const getGameList = async () => {
  let res = await getGame({})
  data.addId = res.data.app_id
  data.liveGameData = res.data.games
  sessionStorage.setItem('OMGtoken', res.data.token)
}
// 获取当前设备信息
const getPhoneInfo = () => {
  var userAgent = navigator.userAgent;
  if (/Android/.test(userAgent)) {
    data.androidShow = true;
  } else {
    data.androidShow = false;
  }
  if (/Windows/.test(userAgent)) {
    data.windowShow = true;
  } else {
    data.windowShow = false;
  }
  if (/iPhone/.test(userAgent)) {
    data.iphoneShow = true;
  } else {
    data.iphoneShow = false;
  }
}
// 选择游戏
const chooseGame = throttle(async (item: any) => {
  if (data.loading) return
  data.loading = true
  // 第三方游戏
  let token = '';
  if(item.value > 10000000){
    let resToken = await userLogin({
      gameid: JSON.stringify(item.value),
      lang: data.gameLang,
      nick: JSON.stringify(item.value),
      appid: JSON.stringify(data.addId),
      token: sessionStorage.getItem('OMGtoken'),
    });
    token = resToken.data.token
  }
  try {
    let res = await playGame({
      gameid: JSON.stringify(item.value),
      lang: data.gameLang,
      nick: JSON.stringify(item.value),
      appid: JSON.stringify(data.addId),
      token: token?token:sessionStorage.getItem('OMGtoken'),
    })
    data.loading = false
    if (res.data.gameurl) {
      if (data.iphoneShow) {
        window.location.href = res.data.gameurl;
      } else {
        window.open(res.data.gameurl);
      }
    } else {
      ElMessage.error('Stay tuned')
    }
  } catch (error) {
    data.loading = false
  }
}, 2000)

// 展示全部游戏
const chooseGameShow = (label: string, data: any) => {
  data.gameTypeName = label
  data.gameTypeList = data
  data.isShowGame = true
  console.log(244,label,data.isShowGame);
}

// 选择游戏类型
const chooseType = (item: any) => {
  data.gameType = item.value
  data.indexBrand = -1
  var element = document.getElementById('indexPage') as any;
  if (element) {
    element.scrollTop = 0
  }
  // Originals【全部】 Slots【PG,JILI,PP】 MiniGame【OMG,SPRIBE】
  GameBrand.forEach((second: any) => {
    second.show = false
    if (item.label == 'Originals') {
      second.show = true
    }
    if (item.label == 'Slots') {
      if (second.label == 'PG' || second.label == 'JILI' || second.label == 'PRAGMATIC') {
        second.show = true
      }
    }
    if (item.label == 'Mini Game') {
      if (second.label == 'OMG' || second.label == 'SPRIBE') {
        second.show = true
      }
    }
  })
}

// 选择游戏品牌
const chooseBrand = (item: any, index: number, game?: number) => {
  data.indexBrand = index
  console.log(item,data.list);
  // // 设置滚动条高度
  // var home = document.getElementById('app') as any;
  // if (item.value == 'OMG') {
  //   home.scrollTop = 490
  // }
  // if (item.value == 'PG') {
  //   if (data.gameType == 1) { // Originals
  //     home.scrollTop = 980
  //   } else {
  //     home.scrollTop = 490 // Slots
  //   }
  // }
  // if (item.value == 'JILI') {
  //   if (data.gameType == 1) { // Originals
  //     home.scrollTop = 1200
  //   } else {
  //     home.scrollTop = 800 // Slots
  //   }
  // }
  // if (item.value == 'SPRIBE') { // SPRIBE
  //   home.scrollTop = 730
  // }
  // if (item.value == 'PRAGMATIC') { // PRAGMATIC
  //   home.scrollTop = 1800
  // }
  // if (game) {
  //   data.searchGame = game
  // }
  // setTimeout(function () {
  //   data.searchGame = -1
  // }, 1000);
}

// 获取滚动条高度
const getScroll = (event: any) => {
  data.scrollTopValue = event.target.scrollTop
}

// 选择多语言
const chooseLang = (item: any) => {
  data.isShowLang = false
  data.gameLang = item.value
}

// 关闭
const handleClose = (done: () => void) => {
  done()
}
// 横向滚动
const scrollContainer = ref();
const handleWheel = (event: any) => {
  // 阻止默认的滚动行为
  event.preventDefault();
  // 获取滚动的方向
  const delta = event.deltaY > 0 ? -250 : 250; // 根据需要可以调整滚动的速度
  // 滚动容器
  scrollContainer.value.scrollBy({
    left: delta,
    behavior: 'smooth'
  })
}
// 游戏的横向
const scrollGame = ref();
const handleGame = (event: any) => {
  console.log(event);
  // 阻止默认的滚动行为
  event.preventDefault();
  // 获取滚动的方向
  const delta = event.deltaY > 0 ? -500 : 500; // 根据需要可以调整滚动的速度
  console.log(scrollGame.value.scrollBy);
  // 滚动容器
  // scrollGame.value.scrollBy({
  //   left: delta,
  //   behavior: 'smooth'
  // })
}

defineExpose({
  FirstMeth,
})
</script>

<style scoped lang="scss">
.gameImg {
  width: 20px;
  border-radius: 12px;
}

.scrollDivStyle {
  overflow-x: scroll;
  overflow-y: hidden;
  white-space: nowrap;
  padding-bottom: 15px;
  font-family: 'Graphik', sans-serif;

  &::-webkit-scrollbar {
    height: 3px;
  }

  &::-webkit-scrollbar-thumb {
    background: var(--color-select);
    border-radius: 20px;
  }
}

.indexPage {
  width: 100%;
  height: 100%;
  padding: 70px 10px 10px;
  font-family: 'Geogtq', sans-serif;
  font-weight: bold;
  user-select: none; //禁止被用户选中
  overflow: auto;

  .header {
    width: 100%;
    padding: 10px 0px 10px 20px;
    background-color: var(--color-background);
    padding-bottom: 10px;
    border-bottom: 1px solid var(--color-index);
    position: fixed;
    top: 0;
    right: 10px;
    z-index: 100;

    .logo {
      width: 70px;
      // height: 25.30px;
    }

    .icon {
      padding: 8px 10px 2px;
      background-color: var(--color-index);
      border-radius: 5px;

      img {
        width: 20px;
      }
    }
  }

  .box_lang {
    width: 200px;
    height: 280px;
    overflow: auto;
    color: #f6f6f6;
    line-height: 30px;
    background-color: var(--color-index);
    border-radius: 5px;
    position: absolute;
    top: 60px;
    right: 10px;
    z-index: 100;

    .lang_item {
      cursor: pointer;
      padding: 5px 10px;

      img {
        width: 30px;
        margin-right: 10px;
      }
    }

    .lang_item:hover {
      background-color: rgba(144, 244, 98, 0.2);
    }
  }

  .box_round {
    width: 100%;
    height: 150px;

    .round_img {
      width: 100%;
      border-radius: 15px;
    }
  }

  // 游戏类型
  .box_type {
    .gameItem {
      min-width: 110px !important;
      height: 85px;
      padding-top: 10px;
      border-radius: 15px;
      margin-right: 5px;
      border: 2px solid var(--color-index);
      background-color: rgba(33, 37, 41, 1);
      box-sizing: border-box;

      /* 防止边框影响尺寸 */
      img {
        width: 30px;
        height: 30px;
      }
    }

    .selectColorMobileType {
      color: var(--color-select);
      border: 2px solid var(--color-select) !important;
      background-color: rgba(144, 244, 98, 0.2) !important;

      img {
        transform: translateY(-41px);
        filter: drop-shadow(var(--color-select) 0 41px);
      }
    }
  }

  .box_brand {
    .magnifying {
      position: absolute;
      top: 18px;
      left: 15px;
    }

    .input {
      width: 100%;
      height: 50px;
    }

    .search {
      width: 50px;
    }

    .game_brand {
      min-width: 110px;
      height: 40px;
      line-height: 40px;
      border-radius: 30px;
      border: 2px solid var(--color-index);
      cursor: pointer;
      overflow: hidden;
      box-sizing: border-box;

      /* 防止边框影响尺寸 */
      img {
        width: 60px;
        height: 30px;
      }
    }

    .selectColorMobileGame {
      color: var(--color-select);
      border: 2px solid var(--color-select) !important;
      background-color: rgba(144, 244, 98, 0.2) !important;
      box-shadow: 0.5px 0.5px 5px var(--color-select);

      img {
        transform: translateY(-31px);
        filter: drop-shadow(var(--color-select) 0 31px);
      }
    }
  }

  .hotGame {
    font-family: 'Geogtq', sans-serif;

    .gameItem {
      width: 150px;
      padding-top: 10px;
      border-radius: 5px;
      margin-right: 5px;

      /* 防止边框影响尺寸 */
      img {
        width: 150px;
        height: 120px;
      }
    }

    .gameTitle {
      font-family: 'Graphik', sans-serif;
      text-align: left;
      font-size: 15px;
      color: #ffffff;
    }

    .remark {
      font-size: 12px;
    }
  }
}
</style>